package com.bmps.springcloud.insuredmicroservice.controller.accept;
import com.alibaba.fastjson.JSON;
import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.MessageModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.PersonModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.AcceptResponseDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.ErrorListModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.ErrorModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.ResponseHeaderModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * @author lyw
 * @date 2019/8/2 14:36
 */
public class InsuranceAcceptSupport {

    public static AcceptResponseDataModel bindingErrors(RequestHeaderModel requestHeaderModel, BindingResult bindingResult) {
        AcceptResponseDataModel acceptResponseDataModel = new AcceptResponseDataModel();
        ResponseHeaderModel headerModel = new ResponseHeaderModel();
        headerModel.setRequestType(requestHeaderModel.getRequestType());
        headerModel.setUUID(requestHeaderModel.getUUID());
        headerModel.setSendTime(requestHeaderModel.getSendTime());
        headerModel.setResponseCode(AcceptConstant.RESPONSE_ERROR_CODE);
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        List<ErrorModel> errorList = new ArrayList<>(fieldErrors.size());
        for (FieldError error : fieldErrors) {
            ErrorModel errorModel = new ErrorModel();
            errorModel.setErrorId(AcceptConstant.RESULT_FAIL_CODE);
            errorModel.setErrorMessage(error.getDefaultMessage());
            errorList.add(errorModel);
        }
        ErrorListModel errorListModel = new ErrorListModel();
        errorListModel.setError(errorList);
        headerModel.setErrorList(errorListModel);
        acceptResponseDataModel.setHeader(headerModel);
        return  acceptResponseDataModel;
    }

    static MessageModel createMessageModel(AcceptRequestDataModel acceptRequestDataModel,
                                                  AcceptResponseDataModel acceptResponseDataModel) {
        MessageModel messageModel = AcceptUtil.getMessage(acceptResponseDataModel, acceptRequestDataModel.getRequest().getPolicyInfo().getContNo());
        messageModel.setMessage(JSON.toJSONString(acceptRequestDataModel));
        messageModel.setOrderNo(acceptRequestDataModel.getRequest().getPolicyInfo().getOrderNo());
        return messageModel;
    }

     static HashSet<PersonModel> getPersonList(CustInfoModel custInfo) {
        HashSet<PersonModel> personModels = new HashSet<>();
        AppntModel appnt = custInfo.getAppnt();
        PersonModel personModel = new PersonModel();
        personModel.setName(appnt.getAppntName());
        personModel.setSex(appnt.getAppntSex());
        personModel.setIdType(appnt.getIdType());
        personModel.setIdNo(appnt.getIdNo());
        personModel.setBirthday(DateUtil.format(appnt.getAppntBirthday(), "yyyyMMdd"));
        AcceptUtil.setPersonModelIdNo(personModel);
        personModels.add(personModel);
        List<InsurantModel> insurantModelList = custInfo.getInsurants().getInsurant();
        if (insurantModelList != null) {
            for (InsurantModel insurantModel : insurantModelList) {
                personModel = new PersonModel();
                personModel.setName(insurantModel.getName());
                personModel.setSex(insurantModel.getSex());
                personModel.setIdType(insurantModel.getIdType());
                personModel.setIdNo(insurantModel.getIdNo());
                personModel.setBirthday(DateUtil.format(insurantModel.getBirthday(), "yyyyMMdd"));
                AcceptUtil.setPersonModelIdNo(personModel);
                personModels.add(personModel);
            }
        }
        BeneficiariesModel beneficiaries = custInfo.getBnfInfo().getBeneficiaries();
        if (beneficiaries != null) {
            List<BeneficiaryModel> beneficiaryModelList = beneficiaries.getBeneficiary();
            if (beneficiaryModelList != null) {
                for (BeneficiaryModel beneficiaryModel : beneficiaryModelList) {
                    personModel = new PersonModel();
                    personModel.setName(beneficiaryModel.getBfcyName());
                    personModel.setSex(beneficiaryModel.getBfcySex());
                    personModel.setIdType(beneficiaryModel.getBfcyIdType());
                    personModel.setIdNo(beneficiaryModel.getBfcyIdCode());
                    personModel.setBirthday(DateUtil.format(beneficiaryModel.getBfcyBirthday(), "yyyyMMdd"));
                    AcceptUtil.setPersonModelIdNo(personModel);
                    personModels.add(personModel);
                }
            }

        }
        return personModels;
    }
    static AcceptResponseDataModel bindingExceptionErrors(RequestHeaderModel requestHeaderModel) {
        AcceptResponseDataModel acceptResponseDataModel = new AcceptResponseDataModel();
        ResponseHeaderModel headerModel = new ResponseHeaderModel();
        headerModel.setRequestType(requestHeaderModel.getRequestType());
        headerModel.setUUID(requestHeaderModel.getUUID());
        headerModel.setSendTime(requestHeaderModel.getSendTime());
        headerModel.setResponseCode(AcceptConstant.RESPONSE_ERROR_CODE);
        List<ErrorModel> errorList = new ArrayList<>(1);
        ErrorModel errorModel = new ErrorModel();
        errorModel.setErrorId(AcceptConstant.RESULT_FAIL_CODE);
        errorModel.setErrorMessage(AcceptConstant.RESULT_FAIL_MESSAGE);
        errorList.add(errorModel);
        ErrorListModel errorListModel = new ErrorListModel();
        errorListModel.setError(errorList);
        headerModel.setErrorList(errorListModel);
        acceptResponseDataModel.setHeader(headerModel);
        return acceptResponseDataModel;
    }

    static AcceptResponseDataModel bankInformationErrors(RequestHeaderModel requestHeaderModel) {
        AcceptResponseDataModel acceptResponseDataModel = new AcceptResponseDataModel();
        ResponseHeaderModel headerModel = new ResponseHeaderModel();
        headerModel.setRequestType(requestHeaderModel.getRequestType());
        headerModel.setUUID(requestHeaderModel.getUUID());
        headerModel.setSendTime(requestHeaderModel.getSendTime());
        headerModel.setResponseCode(AcceptConstant.RESPONSE_ERROR_CODE);

        List<ErrorModel> errorList = new ArrayList<>(1);
        ErrorModel errorModel = new ErrorModel();
        errorModel.setErrorId(AcceptConstant.RESULT_FAIL_CODE);
        errorModel.setErrorMessage(AcceptConstant.BANK_INFORMATION_CHECK_CUSTOM_MESSAGE);
        errorList.add(errorModel);
        ErrorListModel errorListModel = new ErrorListModel();
        errorListModel.setError(errorList);
        headerModel.setErrorList(errorListModel);
        acceptResponseDataModel.setHeader(headerModel);
        return acceptResponseDataModel;
    }
}
